package com.zhangxin.easyexcelutil.controller;

import com.zhangxin.easyexcelutil.model.User;
import com.zhangxin.easyexcelutil.util.ExcelUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangxin on 2021/1/30.
 */
@RestController
public class ExcelController {

    private List<User> data = new ArrayList<>();
    /**
     * 使用EasyExcelUtil导出Excel
     *
     * @throws Exception
     */
    @GetMapping("/exportExcel")
    public ResponseEntity exportExcel(){
        ResponseEntity responseEntity = ExcelUtil.writeExcel(initData(),"导出User信息.xlsx");
        return responseEntity;
    }

    private List<User> initData() {
        if (CollectionUtils.isEmpty(data)) {
            for (int i = 0; i < 1000; i++) {
                User user = new User();
                user.setId(i);
                user.setAge(18+i);
                user.setName("test"+i);
                user.setBirthday(new Date());
                user.setGender(i%2==0?"0":"1");
                data.add(user);
            }
        }
        return data;
    }

}
